#!/bin/bash
function xz(){
echo ------------------------------
echo
echo 1.vd
echo
echo 2.menu
echo
echo ------------------------------
}

function vd(){

cd /media/me/
MapFolder=$(yad --width=800 --height=400  --title="Select map folder" --file-selection --directory)
cd $MapFolder

rm -rf vd.config
file=$(find . -name *.config)
(
echo '#'
echo '#' xish
echo '#' ) >vd.config
num0=0
for i in ${file[@]}
do
	((num0++))
	ii=${i:2}
	echo include '\'${ii//'/'/'\'} >> vd.config
done
echo
echo ------------------------------
cat vd.config
echo ------------------------------
echo

}

function menu(){
cd /media/me/
MapFolder=$(yad --width=800 --height=400  --title="Select map folder" --file-selection --directory)
cd $MapFolder

num0=0
for i in *
do
	((num0++))
#	echo $num0 $i
	files[$num0]=$i
done
#	echo ${files[@]}


if [[ "${files[@]}" =~ vhdx ]]
then
	vdisk='vhdx'

elif [[ "${files[@]}" =~ vhd ]]
then
	vdisk='vhd'

elif [[ "${files[@]}" =~ iso ]]
then
	vdisk='iso'	
else
	echo 找不到文件
	read -p "按回车退出"
	exit

fi

echo 虚拟磁盘格式--------------------
echo $vdisk

echo 图标文件-----------------------
icon='png'
for str in "*.$icon"
do
	icon=$str
done
num1=0
for a in $icon
do
	((num1++))
	echo $num1 $a
	a[num1]=$a
done
icon=${a[1]}
while (("$num1" > 1))
do
	read -p "请选择：" num11
	if [[ $num11 > $num1 || $num11 < 1 ]]
	then
		echo 输入错误，请重新输入
	else
		icon=${a[$num11]}
		break
	fi
done
echo $icon
echo 虚拟磁盘文件---------------------

for str in "*.$vdisk"
do
	vdisk=$str
done
num2=0
for b in $vdisk
do
	((num2++))
	echo $num2 $b
	b[num2]=$b
done
vdisk=${b[1]}
while (("$num2" > 1))
do
	read -p "请选择：" num22
	if [[ $num22 > $num2 || $num22 < 1 ]]
	then
		echo 输入错误，请重新输入
	else
		vdisk=${b[$num22]}
		break
	fi
done
echo $vdisk
echo ------------------------------

menu=${vdisk%%.*}

(echo '#'
echo '# xish'
echo '#'
echo menuentry '"'$menu'"' {
echo '	icon "'$icon'"'
echo '	vdisk "'$vdisk'"'
echo '	vdtype '${vdisk#*.}
if [[ "${vdisk:0:3}" == "Win" || "${vdisk:0:3}" == "win" && "${vdisk#*.}" != "iso" ]]
then 
	echo '	ostype windows' 
fi

echo '	}' 
echo ) > menu.config
cat menu.config
echo ------------------------------

}


xz
num11=0
while (("$num11" < 10))
do
	read -p "请选择：" num11
	if [[ $num11 = 1 ]]
	then
		vd
		read -p "按回车退出"
		xz

	elif [[ $num11 = 2 ]]
	then
		menu
		read -p "按回车退出"
		xz
	fi
done


